package com.byb.server.service.impl;

import com.byb.server.service.CoverGuideService;
import com.byb.server.domain.CoverGuide;
import com.byb.server.repository.CoverGuideRepository;
import com.byb.server.repository.search.CoverGuideSearchRepository;
import com.byb.server.service.dto.CoverGuideDTO;
import com.byb.server.service.mapper.CoverGuideMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * Service Implementation for managing CoverGuide.
 */
@Service
@Transactional
public class CoverGuideServiceImpl implements CoverGuideService{

    private final Logger log = LoggerFactory.getLogger(CoverGuideServiceImpl.class);

    private final CoverGuideRepository coverGuideRepository;

    private final CoverGuideMapper coverGuideMapper;

    private final CoverGuideSearchRepository coverGuideSearchRepository;
    public CoverGuideServiceImpl(CoverGuideRepository coverGuideRepository, CoverGuideMapper coverGuideMapper, CoverGuideSearchRepository coverGuideSearchRepository) {
        this.coverGuideRepository = coverGuideRepository;
        this.coverGuideMapper = coverGuideMapper;
        this.coverGuideSearchRepository = coverGuideSearchRepository;
    }

    /**
     * Save a coverGuide.
     *
     * @param coverGuideDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public CoverGuideDTO save(CoverGuideDTO coverGuideDTO) {
        log.debug("Request to save CoverGuide : {}", coverGuideDTO);
        CoverGuide coverGuide = coverGuideMapper.toEntity(coverGuideDTO);
        coverGuide = coverGuideRepository.save(coverGuide);
        CoverGuideDTO result = coverGuideMapper.toDto(coverGuide);
        //coverGuideSearchRepository.save(coverGuide);
        return result;
    }

    /**
     *  Get all the coverGuides.
     *
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<CoverGuideDTO> findAll(Pageable pageable) {
        log.debug("Request to get all CoverGuides");
        return coverGuideRepository.findAll(pageable)
            .map(coverGuideMapper::toDto);
    }

    /**
     *  Get one coverGuide by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public CoverGuideDTO findOne(Long id) {
        log.debug("Request to get CoverGuide : {}", id);
        CoverGuide coverGuide = coverGuideRepository.findOne(id);
        return coverGuideMapper.toDto(coverGuide);
    }

    /**
     *  Delete the  coverGuide by id.
     *
     *  @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete CoverGuide : {}", id);
        coverGuideRepository.delete(id);
        coverGuideSearchRepository.delete(id);
    }

    /**
     * Search for the coverGuide corresponding to the query.
     *
     *  @param query the query of the search
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<CoverGuideDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of CoverGuides for query {}", query);
        Page<CoverGuide> result = coverGuideSearchRepository.search(queryStringQuery(query), pageable);
        return result.map(coverGuideMapper::toDto);
    }
}
